﻿var btnPrev;
var btnPlayPause;
var btnNext;
var stylePlay;
var stylePause;

var podcastIndex = 0;
var podcasts;
var allPodcasts =[
				["http://www.carlsandburgvisits.com/Music/June%202008/10_greenskeepers_-_vagabond-rain.mp3", "GTA Greenskeepers"],
				["http://ampmppc.com/personal/gtasa/06-Da-Shootaz---Grand-Theft-Auto.mp3", "Da Shootaz"],
                ["http://galagame.ru/mp3/gta4-libertycity.mp3", "GTA4 Liberty City"],
				["http://stuffgodhates.com/wp-content/music/God.Hates.You.mp3","The Theme for Michael"],
				["http://ampmppc.com/personal/gtasa/KidFrost-LaRaza.mp3","GTA San Andreas Kid Frost"],
				["http://dl.qq.com/gamezone/d09/tvgame/GTASA/01/109-dazz_band-let_it_whip-musiq.mp3","Let It Whip Dazz Band Grand Theft Auto: San Andreas"],
				["http://dl.qq.com/gamezone/d09/tvgame/GTASA/06/609-pliers-bam_bam-musiq.mp3","Bam Pliers Grand Theft Auto: San Andreas"],
				["http://dl.qq.com/gamezone/d09/tvgame/GTASA/01/105-cameo-candy-musiq.mp3","Candy Cameo Grand Theft Auto: San Andreas"],
				["http://dl.qq.com/gamezone/d09/tvgame/GTASA/01/112-maze-twilight-musiq.mp3","Twilight Maze Grand Theft Auto: San Andreas"],
				["http://www.wuyue0425.com/music/HollywoodSwingin.mp3","Hollywood Swingin&pos; Kool And The Gang"],
				["http://dl.qq.com/gamezone/d09/tvgame/GTASA/07/705-grand_funk_railroad-some_kind_of_wonderful-musiq.mp3","Some Kind Of Wonderful"]
                ];
var connectedShowPodcasts = [
                ];
var msdnFlashPodcasts = [
                ];
var otherPodcasts = [
                ];

function initApp() {	
	initButtons();

    retrieveQueryString();

    if (queryStringParams["topic"]) {
        var topicValue = unescape(queryStringParams["topic"]);
        if (topicValue == "msdnFlash") {
            podcasts = msdnFlashPodcasts;
        }
        else if (topicValue == "connectedShow") {
            podcasts = connectedShowPodcasts;
        }
        else if (topicValue == "other") {
            podcasts = otherPodcasts;
        }
    }
    else {
        podcasts = allPodcasts;
    }

    selectPodcast(0);
}


function initButtons() {
	try {
		// Developer sample code
		if(window.external.msIsSiteMode()) {
			

			// Add buttons
			btnPrev = window.external.msSiteModeAddThumbBarButton('../images/prev.ico', 'Previous');
			btnPlayPause = window.external.msSiteModeAddThumbBarButton('../images/play.ico', 'Play');
			btnNext = window.external.msSiteModeAddThumbBarButton('../images/next.ico', 'Next');

			// Add styles
			stylePlay = window.external.msSiteModeAddButtonStyle(btnPlayPause, '../images/play.ico', "Play");
			stylePause = window.external.msSiteModeAddButtonStyle(btnPlayPause, '../images/pause.ico', "Pause");
			
			// Show thumbar
			window.external.msSiteModeShowThumbBar();
			// Add buttons listener
			if (typeof window.addEventListener != 'undefined') {
  window.addEventListener('msthumbnailclick',onButtonClicked,false);
} else {
  window.attachEvent('msthumbnailclick',onButtonClicked);
}			
		}
	}
	catch(e) {
		// fail silently
	}
}

function selectPodcast(podcastID) {

  
	var audioNew = document.getElementById("audio_" + podcastID);
	var audioOld = document.getElementById("audio_" + podcastIndex);


	// Remember state of player
	var wasPaused = audioOld.paused;
	

	// Stop current audio
	try {
	    if (audioOld.canPlayType("audio/mp3")) {
	        audioOld.pause();
	        audioOld.currentTime = 0;
	    }
	    else {
	   
	    }
	}
	catch(e) {
		// audio failure - fail silently
	}

	if(!wasPaused) {
	    if (audioNew.canPlayType("audio/mp3")) {
	        audioNew.play();
	    }
	    else {
	    
	    }
	}
	
	// Update thumb bar buttons
	updatePlayPauseButton(podcastID);
    updateNextAndPrevButtons(podcastID);
	
    podcastIndex = podcastID;
}

function updatePlayPauseButton(podcastID) {
	var audio = document.getElementById("audio_" + podcastID);

	try {
		// Developer sample code
		if(window.external.msIsSiteMode()) {
			if (audio.paused) {
				window.external.msSiteModeShowButtonStyle(btnPlayPause, stylePlay);
			}
			else {
				window.external.msSiteModeShowButtonStyle(btnPlayPause, stylePause);
			}
		}
	}
	catch(e) {
		// fail silently
	}
}

function updateNextAndPrevButtons(podcastID) {
	try {
		if(window.external.msIsSiteMode()) {
			window.external.msSiteModeUpdateThumbBarButton(btnNext, true, true);
			window.external.msSiteModeUpdateThumbBarButton(btnPrev, true, true);

			if (podcastID == 0) {
				window.external.msSiteModeUpdateThumbBarButton(btnPrev, false, true);
			}
			else if (podcastID == podcasts.length - 1) {
				window.external.msSiteModeUpdateThumbBarButton(btnNext, false, true);
			}
		}
	}
	catch(e) {
		// fail silently
	}
}


function onButtonClicked(e) {
    switch (e.buttonID) {
        case btnPrev:
            prev();
            break;
        case btnPlayPause:
            playPause();
            break;
        case btnNext:
            next();
            break;
    }
}

function prev() {
    selectPodcast(podcastIndex - 1);
}

function playPause() {
	var audio = document.getElementById("audio_" + podcastIndex);

	if (audio.canPlayType("audio/mp3")) {
	    if (audio.paused) {
	        audio.play();
	    }
	    else {
	        audio.pause();
	    }
	}
	else {
	}
    
    updatePlayPauseButton(podcastIndex);
}

function next() {
    selectPodcast(podcastIndex + 1);
}

var queryStringParams = new Array();

function retrieveQueryString() {
    var query = window.location.search.substring(1);
    var parms = query.split('?');

    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos);
            var val = parms[i].substring(pos + 1);
            queryStringParams[key] = val;
        }
    }
}
